package com.aimosheng.main.activity;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aimosheng.common.CommonAppConfig;
import com.aimosheng.common.activity.AbsActivity;
import com.aimosheng.common.bean.UserBean;
import com.aimosheng.common.event.CoinChangeEvent;
import com.aimosheng.common.interfaces.CommonCallback;
import com.aimosheng.common.utils.ClickUtil;
import com.aimosheng.common.utils.RouteUtil;
import com.aimosheng.common.utils.ScreenDimenUtil;
import com.aimosheng.common.utils.WordUtil;
import com.aimosheng.main.R;
import com.aimosheng.main.http.MainHttpConsts;
import com.aimosheng.main.http.MainHttpUtil;
import com.mob.adsdk.AdSdk;
import com.scwang.smartrefresh.layout.util.DensityUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by cxf on 2019/4/10.
 */

public class WalletActivity extends AbsActivity {

    private static final String TAG = "WalletActivity";
    private TextView mCoinName;
    private TextView mCoin;
    private boolean mPaused;
    private boolean mCoinChanged;
    private ViewGroup mContainer;
    private AdSdk.BannerAd mBannerAd;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_wallet;
    }

    @Override
    protected void main() {
        setTitle(WordUtil.getString(R.string.wallet));
        mContainer = findViewById(R.id.container); //【重要】container 必须是 RelativeLayout，否则会有部分广告展示不全
        mCoinName = findViewById(R.id.coin_name);
        mCoin = findViewById(R.id.coin);
        mCoinName.setText(String.format(WordUtil.getString(R.string.wallet_coin_name), CommonAppConfig.getInstance().getCoinName()));
        EventBus.getDefault().register(this);
        getLastCoin();
        destroyBannerAd();
        loadBannerAd();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mPaused = true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mPaused && mCoinChanged) {
            mCoinChanged = false;
            getLastCoin();
        }
        mPaused = false;
    }

    /**
     * 获取余额
     */
    private void getLastCoin() {
        MainHttpUtil.getBaseInfo(new CommonCallback<UserBean>() {
            @Override
            public void callback(UserBean u) {
                if (mCoin != null) {
                    mCoin.setText(u.getCoin());
                }
            }
        });
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCoinChangeEvent(CoinChangeEvent e) {
        mCoinChanged = true;
    }

    public void walletClick(View v) {
        if(!ClickUtil.canClick()){
            return;
        }
        int i = v.getId();
        if (i == R.id.btn_charge) {
            RouteUtil.forwardMyCoin();
        } else if (i == R.id.btn_detail) {
            WalletDetailActivity.forward(mContext);
        } else if (i == R.id.btn_profit) {
            MyProfitActivity.forward(mContext);
        }
    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        MainHttpUtil.cancel(MainHttpConsts.GET_BASE_INFO);
        super.onDestroy();
        destroyBannerAd();
    }

    private void loadBannerAd() {
        // 计算 Banner 广告宽高，单位dp
        float screenWidth = ScreenDimenUtil.getInstance().getScreenWdith();
        float padding = DensityUtil.dp2px(15);
        float width = screenWidth - padding*2;
        float height = width / 6.4f; // 推荐宽高比6.4:1

        AdSdk.getInstance().loadBannerAd(this, "b1", mContainer, width, height,
                new AdSdk.BannerAdListener() {
                    @Override
                    public void onAdLoad(String id, AdSdk.BannerAd ad) {
                        Log.d(TAG, "BannerAd onAdLoad");
                        // 设置刷新频率，为0或30~120之间的整数，单位秒，0表示不自动轮播，默认30秒
                        ad.setRefreshInterval(30);
                        mBannerAd = ad;
                    }

                    @Override
                    public void onAdShow(String id) {
                        Log.d(TAG, "BannerAd onAdShow");
                    }

                    @Override
                    public void onAdClose(String id) {
                        Log.d(TAG, "BannerAd onAdClose");
                    }

                    @Override
                    public void onAdClick(String id) {
                        Log.d(TAG, "BannerAd onAdClick");
                    }

                    @Override
                    public void onError(String id, int code, String message) {
                        Log.d(TAG, "BannerAd onError: code=" + code + ", message=" + message);
                    }
                });
    }

    private void destroyBannerAd() {
        if (null != mBannerAd) {
            mBannerAd.destroy();
            mBannerAd = null;
        }
    }
}
